package com.frontop.system.modules.sale.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.frontop.common.domain.UserJurisdiction;
import com.frontop.common.util.JwtUtil;
import com.frontop.common.util.ResultJsonUtil;
import com.frontop.system.modules.sale.model.SaleInfo;
import com.frontop.system.modules.sale.model.SaleQrInfo;
import com.frontop.system.modules.sale.service.SaleInfoService;
import com.frontop.system.modules.sale.service.SaleQrInfoService;
import com.frontop.system.modules.sale.vo.SaleInfoVO;
import com.frontop.system.modules.sale.vo.SaleQrInfoVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 二维码信息 前端控制器
 * </p>
 *
 * @author YangJia
 * @since 2022-04-24
 */
@RestController
@RequestMapping("/sale-qr-info")
public class SaleQrInfoController {
    @Autowired
    private SaleQrInfoService saleQrInfoService;

    @Autowired
    private SaleInfoService saleInfoService;

    @ApiOperation(value = "二维码链接列表")
    @GetMapping("/qrList/{saleId}")
    public ResultJsonUtil<Object> qrList(@PathVariable("saleId") Integer saleId,
                                               Authentication authentication) {
        SaleInfo saleInfo = saleInfoService.getById(saleId);

        SaleQrInfoVO saleQrInfoVO = new SaleQrInfoVO();
        saleQrInfoVO.setOrganization(saleInfo.getOrganization());
        saleQrInfoVO.setSaleUuid(saleInfo.getUuid());

        List<SaleQrInfo> saleQrInfoList = saleQrInfoService.qrListPage(saleQrInfoVO);
        return new ResultJsonUtil().success(saleQrInfoList);
    }


    @ApiOperation(value = "二维码列表")
    @PostMapping("/qrListPageIndex")
    public ResultJsonUtil<Object> qrListPageIndex(@RequestBody(required = false) SaleQrInfoVO vo,
                                               Authentication authentication) {
        //获取当前登录用户基本权限信息
        UserJurisdiction userJurisdiction = JwtUtil.getUserJurisdiction(authentication);
        vo.setLowerOrganizationIds(userJurisdiction.getLowerOrganizationIds());
        Page<SaleQrInfo> page = new Page<>(vo.getPageNo(), vo.getPageSize());
        page = saleQrInfoService.qrListPageIndex(page,vo);
        return new ResultJsonUtil().success(page);
    }


    @ApiOperation(value = "修改或者添加新的二维码")
    @PostMapping("/saveOrUpdate")
    public ResultJsonUtil<Object> saveOrUpdate(@RequestBody(required = false) SaleQrInfo saleQrInfo,
                                               Authentication authentication) {


        if(saleQrInfo.getId() == null || saleQrInfo.getId().equals(0)){
            saleQrInfo.setCreateTime(new Date());
            saleQrInfo.setStatus(1);
            String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
            saleQrInfo.setUuid(uuid);
        }

        QueryWrapper<SaleQrInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("address",saleQrInfo.getAddress());
        queryWrapper.eq("organization",saleQrInfo.getOrganization());
        queryWrapper.eq("status",1);
        SaleQrInfo kuData = saleQrInfoService.getOne(queryWrapper);
        if(kuData != null ){
            if((saleQrInfo.getId() == null || saleQrInfo.getId().equals(0)) ||(!kuData.getId().equals(saleQrInfo.getId()))){
                return new ResultJsonUtil().customFail("该企业以存在相同的二维码信息");
            }
        }
        if(saleQrInfo.getId() != null){
            //编辑不能修改所属企业
            saleQrInfo.setOrganization(null);
        }



        saleQrInfoService.saveOrUpdate(saleQrInfo);
        return new ResultJsonUtil().success("请求成功");
    }

    @ApiOperation(value = "删除二维码")
    @GetMapping("/deleteSaleQr/{id}")
    public ResultJsonUtil<Object> deleteSaleQr(@PathVariable("id") Integer id,
                                             Authentication authentication) {
        SaleQrInfo saleInfo = new SaleQrInfo();
        saleInfo.setStatus(0);
        saleInfo.setId(id);
        saleQrInfoService.updateById(saleInfo);
        return new ResultJsonUtil().success("请求成功");
    }

}
